//
//  MQTabBarItem.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/26.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class MQTabBarItem: UITabBarItem {
    
    override var badgeValue: String?{
        
        didSet{
        
            guard let tabBarController = valueForKeyPath("_target") as? UITabBarController else {
                return
            }
            let tabBar = tabBarController.tabBar
        
            for tabBarChild in tabBar.subviews
            {
                // 如果是 UITabBarButton
                if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!)
                {
                    // 继续遍历
                    for tabBarButtonChild in tabBarChild.subviews
                    {
                        // 如果是 _UIBadgeView
                        if tabBarButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!)
                        {
                        // 继承遍历
                            for badgeViewChild in tabBarButtonChild.subviews
                            {
                                if badgeViewChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!)
                                {
                                        badgeViewChild.setValue(UIImage(named: "main_badge"), forKeyPath: "_image")
            
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
